<?php

class default_DangnhapController extends ZF_DefaultController {

    public function indexAction() {
        
    }

    public function loginAction() {
        $username = $this->getRequest()->getParam('username');
        $password = $this->getRequest()->getParam('password');
        $auth = Zend_Auth::getInstance();
        $db = Zend_Registry::get("db");
        $authAdapter = new Zend_Auth_Adapter_DbTable($db, "user", "username", "pw");


        $authAdapter->setIdentity($username);
        $authAdapter->setCredential(md5($password));

        $result = $auth->authenticate($authAdapter);

        if ($result->isValid()) {
            $data_auth = $authAdapter->getResultRowObject(null, array('pw'));
            $auth->getStorage()->write($data_auth);
            $this->view->profileNameSpace = (array) $data_auth;
            $data = array('error' => '0');
            
            if($this->view->profileNameSpace['isAdmin'] == "0"){
                $this->_redirect("/default/tracuutongquat");
            }
            else if($this->view->profileNameSpace['isAdmin'] == "1"){
                $this->_redirect("/default/donhang  ");
            }
            else{
                $this->_redirect("/default/tracuu");
            }
            $this->_redirect("/");
        } else {
            $this->_redirect("/dangnhap/index");
        }
    }

    public function logoutAction() {
        $profileNameSpace = new Zend_Session_Namespace('profile');
        $profileNameSpace->user = null;

        Zend_Auth::getInstance()->clearIdentity();
        $this->_redirect("/index");
    }

}
